package cn.crce.cloud9.simple;

import javax.swing.*;
import java.awt.*;

/**
 * @author OnCloud9
 * @description
 * @project NRCE-TEST
 * @date 2022年09月10日 上午 10:20
 */
public class Case36 {
    int grades[][] = { { 77, 68, 86, 73 },
            { 96, 87, 89, 81 },
            { 70, 90, 86, 81 } };
    int students, exams;
    String output;
    JTextArea outputArea;

    public Case36(){
        students = grades.length;
        exams = grades[ 0 ].length;

        JFrame f = new JFrame();
        f.setSize(300,300);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //*********Found**********
        outputArea = new JTextArea();
        Container c = f.getContentPane();
        //*********Found**********
        c.add( outputArea );

        output = "数组是:";
        buildString();
        output += "\n\n最高分: " + maximum() + "\n";
        //*********Found**********
        for ( int i = 0; i < grades.length; i++ )
            output += "\n第" + (i+1) + "个学生的平均分是: " +
                    average( grades[ i ] );
        //*********Found**********
        outputArea.setText( output );
    }


    //找最高分
    public int maximum(){
        int highGrade = 0;
        for ( int i = 0; i < students; i++ )
            for ( int j = 0; j < exams; j++ )
                if ( grades[ i ][ j ] > highGrade )
                    //*********Found**********
                    highGrade = grades[ i ][ j ];
        return highGrade;
    }
    //对各组学生确定平均分
    public int average( int setOfGrades[] ){
        int total = 0;
        for ( int i = 0; i < setOfGrades.length; i++ )
            //*********Found**********
            total += setOfGrades[i];

        return total /exams;
    }
    //输出格式
    public void buildString(){
        output += "        ";
        for ( int i = 0; i < exams; i++ )
            output += "[" + i + "]   ";
        for ( int i = 0; i < students; i++ ) {
            output += "\ngrades[" + i + "]   ";
            for ( int j = 0; j < exams; j++ )
                output += grades[ i ][ j ] + "   ";
        }
    }

    public static void main(String[ ]args){
        new Case36();
    }
}
